package com.med.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.TeaException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class IdentificationOCRUtil {
    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;

//    OCR识别输入地址获取姓名、性别、种族、出生日期、地址、身份证号信息
    public  HashMap<String, String> getIdentificationCard(String path)  {
        try {
            com.aliyun.ocr_api20210707.Client client = createClient();
            java.io.InputStream bodyStream = com.aliyun.darabonba.stream.Client.readFromFilePath(path);
            com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest recognizeIdcardRequest = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest()
                    .setUrl("")
                    .setBody(bodyStream);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse resp = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);

            JSONObject jsonObject = JSON.parseObject(resp.getBody().getData())
                    .getJSONObject("data")
                    .getJSONObject("face")
                    .getJSONObject("data");
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("name",jsonObject.getString("name"));
            hashMap.put("sex",jsonObject.getString("sex"));
            hashMap.put("ethnicity",jsonObject.getString("ethnicity"));
            hashMap.put("birthDate",jsonObject.getString("birthDate"));
            hashMap.put("address",jsonObject.getString("address"));
            hashMap.put("idNumber",jsonObject.getString("idNumber"));
            return hashMap;
        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

    public  com.aliyun.ocr_api20210707.Client createClient() throws Exception {
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential)
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = endpoint;
        return new com.aliyun.ocr_api20210707.Client(config);
    }
}
